Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
The gzip-size npm package allows you to easily calculate the compressed size of a string or buffer using gzip compression. This can be particularly useful for assessing the size of files or assets in web development, ensuring that your resources are optimized for performance by evaluating their size after compression.
Get the compressed size of a string
This feature allows you to calculate the gzip-compressed size of a string. It's useful for quickly assessing how much space a piece of text will take up after compression.
const gzipSize = require('gzip-size');
const string = 'Hello, world!';
gzipSize(string).then(size => {
console.log(`Compressed size: ${size} bytes`);
});
Get the compressed size of a file
This feature calculates the gzip-compressed size of a file's contents. It's particularly useful for web developers looking to optimize their assets for performance by understanding the size impact of compression.
const gzipSize = require('gzip-size');
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) {
throw err;
}
gzipSize(data).then(size => {
console.log(`Compressed size: ${size} bytes`);
});
});
Synchronous API for getting compressed size
gzip-size also offers a synchronous API, allowing for immediate calculation of the compressed size without dealing with promises. This can be handy for scripts and applications where asynchronous handling is not necessary or desired.
const gzipSize = require('gzip-size');
const string = 'Hello, world!';
const size = gzipSize.sync(string);
console.log(`Compressed size: ${size} bytes`);
The 'compressing' package is a comprehensive compression library that supports gzip, tar, zip, and tgz formats. While it offers broader functionality in terms of supported formats, it might be more complex to use when you only need to calculate gzip sizes.
Node's built-in 'zlib' module provides compression functionality, including gzip compression. While it is not an npm package, it serves a similar purpose. However, 'gzip-size' simplifies the process specifically for calculating the size of compressed content, making it more convenient for this specific use case.
Similar to 'gzip-size', 'brotli-size' calculates the size of a string or buffer after compression, but using the Brotli algorithm instead of gzip. This package is useful for those looking to compare or utilize Brotli compression, which can offer better compression ratios for certain types of content.
Get the gzipped size of a string or buffer
$ npm install --save gzip-size
var gzipSize = require('gzip-size');
var string = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';
console.log(string.length);
//=> 191
console.log(gzipSize.sync(string));
//=> 78
Required
Type: string
, buffer
Required
Type: function
Returns the size.
Required
Type: string
, buffer
$ npm install --global gzip-size
$ gzip-size --help
Usage
gzip-size <file>
cat <file> | gzip-size
Example
gzip-size index.js
211
Combine it with pretty-bytes to get a human readable output:
$ pretty-bytes $(gzip-size jquery.min.js)
29.34 kB
MIT © Sindre Sorhus
FAQs
Get the gzipped size of a string or buffer
The npm package gzip-size receives a total of 6,542,214 weekly downloads. As such, gzip-size popularity was classified as popular.
We found that gzip-size demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.